package controller.expertInfo.adminManagerSchedule;


import base.response.OffsetLimitPage;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.expertInfo.ExpertBaseServices;
import services.expertInfo.ScheduleInfoServices;
import services.specific.AdminManageScheduleServices;
import validator.EndTimeFluentValidator;
import validator.NotBlankFluentValidator;
import wt.entity.expertInfo.ScheduleInfo;

import java.util.Map;

/**
 * Created by Administrator on 2017/12/2.
 */
@Controller

@RequestMapping("expertInfo/adminManagerSchedule")
public class AdminManagerScheduleController {
    @Autowired
    ScheduleInfoServices scheduleInfoServices;
    @Autowired
    ExpertBaseServices expertBaseServices;
    @Autowired
    AdminManageScheduleServices adminManageScheduleServices;


    @RequiresPermissions("expertInfo:adminManagerSchedule:default")
    @RequestMapping("index")
    public String index(Map map) throws Exception {
        return "expertInfo/adminManagerSchedule/index";
    }

    @RequiresPermissions("expertInfo:adminManagerSchedule:default")
    @RequestMapping("select")
    @ResponseBody
    public OffsetLimitPage select(String expertName, String idCard, Integer limit, Integer offset) throws Exception {

        if (StringUtils.isBlank(expertName)) {
            expertName=null;
        }else {
            expertName=expertName.trim();
        }

        if (StringUtils.isBlank(idCard)) {
            idCard=null;
        }else {
            idCard=idCard.trim();
        }

      return   adminManageScheduleServices.select(expertName,idCard,offset,limit);
    }

    @RequiresPermissions("expertInfo:adminManagerSchedule:default")
    @RequestMapping("editDlg")
    public String editDlg(Map map) throws Exception {

        map.put(StaticConstant.EDIT_URL, "expertInfo/adminManagerSchedule/insert");
        map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        return "expertInfo/adminManagerSchedule/editDlg";
    }
    @RequiresPermissions("expertInfo:adminManagerSchedule:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(ScheduleInfo scheduleInfo) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(scheduleInfo.getStartTime(), new EndTimeFluentValidator(scheduleInfo.getStartTime(),"开始时间","结束时间"))
                .on(scheduleInfo.getReason(), new NotBlankFluentValidator("原因"))
                .on(scheduleInfo.getExpertId(), new NotBlankFluentValidator("专家姓名"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        commonResult.setStatus(scheduleInfoServices.insertSelective(scheduleInfo));
        return commonResult;
    }

    @RequiresPermissions("expertInfo:adminManagerSchedule:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        commonResult.setStatus(scheduleInfoServices.deleteByPrimaryKey(id));
        return commonResult;
    }


}
